﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SkiPathFinder
{
    public partial class PathNameForm : Form
    {
        public PathNameForm()
        {
            InitializeComponent();

            txtPathName.Text = "path" + FindNewPathNumber();
        }

        /// <summary>
        /// The ski path name entered by user
        /// </summary>
        public string PathName
        {
            get { return txtPathName.Text; }
        }

        /// <summary>
        /// Finds the new available path number
        /// </summary>
        /// <returns></returns>
        private int FindNewPathNumber()
        {
            //set the path name text
            string pathFolder = Globals.SelectedAreaFolder;
            string[] pathShapefiles = Directory.GetFiles(pathFolder, "*.shp");
            int pathNumber = 0;
            foreach (string pathFileName in pathShapefiles)
            {
                string pathName = Path.GetFileNameWithoutExtension(pathFileName);
                string digits = String.Empty;
                for (int j = pathName.Length - 1; j >= 0; j--)
                {
                    if (Char.IsDigit(pathName[j]))
                    {
                        digits = pathName[j] + digits;
                    }
                    else
                    {
                        break;
                    }
                }
                if (digits.Length > 0)
                {
                    int curPathNumber = Convert.ToInt32(digits);
                    if (curPathNumber > pathNumber)
                    {
                        pathNumber = curPathNumber;
                    }
                }
            }
            return pathNumber + 1;
        }
    }
}
